package com.ltw.playit.web.server.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ltw.playit.common.bean.Criteria;
import com.ltw.playit.common.bean.FrontMessage;
import com.ltw.playit.common.utils.JsonConverter;
import com.ltw.playit.web.server.entity.BaseUsers;
import com.ltw.playit.web.server.service.BaseUsersService;

@Controller
public class LoginController {

	@Autowired
	private BaseUsersService baseUsersService;

	@Autowired
	private JsonConverter jsonConverter;

	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

	/**
	 * 用户登录
	 * 
	 * @throws JsonProcessingException
	 * @throws JSONException
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public Object login(@RequestBody String body, HttpSession session, HttpServletRequest request) {

		BaseUsers user = jsonConverter.covertToObject(body, BaseUsers.class);

		if (StringUtils.isBlank(user.getUserName())) {
			return new FrontMessage(false, "帐号不能为空！");
		}
		if (StringUtils.isBlank(user.getPassword())) {
			return new FrontMessage(false, "密码不能为空！");
		}

		Criteria criteria = new Criteria();
		criteria.put("userName", user.getUserName());
		criteria.put("passwordIn", user.getPassword());
		criteria.put("loginip", this.getIpAddr(request));

		String result = this.baseUsersService.login(criteria);
		if ("01".equals(result)) {
			BaseUsers baseUser = (BaseUsers) criteria.get("baseUser");

			logger.info("{}登陆成功", baseUser.getRealName());
			return new FrontMessage(true, "success");
		} else if ("00".equals(result)) {
			return new FrontMessage(false, "用户名或者密码错误!");
		} else {
			return new FrontMessage(false, result);
		}

	}

	/**
	 * 取得客户端真实ip
	 * 
	 * @param request
	 * @return 客户端真实ip
	 */
	public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("X-Forwarded-For");
		logger.debug("1- X-Forwarded-For ip={}", ip);
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
			logger.debug("2- Proxy-Client-IP ip={}", ip);
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
			logger.debug("3- WL-Proxy-Client-IP ip={}", ip);
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
			logger.debug("4- HTTP_CLIENT_IP ip={}", ip);
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
			logger.debug("5- HTTP_X_FORWARDED_FOR ip={}", ip);
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
			logger.debug("6- getRemoteAddr ip={}", ip);
		}
		logger.info("finally ip={}", ip);
		return ip;
	}

}
